package com.gpframework.module.ctn.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gpframework.common.utils.StringUtils;
import com.gpframework.module.ctn.domain.*;
import com.gpframework.module.ctn.domain.req.GpNigeriaMarketCheckAdd;
import com.gpframework.module.ctn.domain.vo.GpNigeriaMarketCheckExcel;
import com.gpframework.module.ctn.domain.vo.GpNigeriaMarketCheckVo;
import com.gpframework.module.ctn.mapper.GpNigeriaMarketCheckMapper;
import com.gpframework.module.ctn.service.IGpNigeriaMarketCheckService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
/**
 * @ClassName GpNigeriaMarketCheck
 * @Author gpframework
 * @Date 2024-01-09
 * @Version 1.0.0
 * @Description 货柜到市场核对Service业务层处理
 */
@Service
public class GpNigeriaMarketCheckServiceImpl extends ServiceImpl<GpNigeriaMarketCheckMapper, GpNigeriaMarketCheck> implements IGpNigeriaMarketCheckService {

    @Autowired
    private GpCtnOrderServiceImpl gpCtnOrderService;

    @Autowired
    private GpSackInformationServiceImpl gpSackInformationService;
    @Autowired
    private GpPackagesInformationServiceImpl gpPackagesInformationService;

    /**
     * @Description 查询列表返回VO 用于返回给前端的列表接口
     * @Author gpframework
     * @Date 2024-01-09
     * @Param [lqw]
     * @Return List<GpNigeriaMarketCheckVo>
     */
    @Override
    public List<GpNigeriaMarketCheckVo> listVo(QueryWrapper<GpNigeriaMarketCheck> lqw) {
        return this.baseMapper.listVo(lqw);
    }

    /**
     * @Description 通过查询详情VO 用于返回给前端的列详情接口
     * @Author gpframework
     * @Date 2024-01-09
     * @Param [id]
     * @Return GpNigeriaMarketCheckVo
     */
    @Override
    public GpNigeriaMarketCheckVo getVoById(Long id) {
        return this.baseMapper.getVo(id);
    }

    @Override
    public List<GpNigeriaMarketCheckExcel> gpNigeriaMarketCheckImport(List<GpNigeriaMarketCheckExcel> excelList, Long brandId, Long storeId) {
        //导入失败的列表
        List<GpNigeriaMarketCheckExcel> failureList = new ArrayList<>();
        //货柜到市场核对
        GpNigeriaMarketCheck gpNigeriaMarketCheck;

        for (int i = 0; i < excelList.size(); i++) {
            GpNigeriaMarketCheckExcel excel = excelList.get(i);
            if (StringUtils.isNull(excel)) {
                excel = new GpNigeriaMarketCheckExcel();
                excel.setSubscript(i+1);
                excel.setMsg("读取数据失败");
                failureList.add(excel);
                continue;
            }
            gpNigeriaMarketCheck = Convert.convert(new TypeReference<GpNigeriaMarketCheck>() {}, excel);
            this.save(gpNigeriaMarketCheck);
        }
        return failureList;
    }

    @Override
    public boolean addBatch(GpNigeriaMarketCheckAdd gpNigeriaMarketCheckAdd) {
        List<GpNigeriaMarketCheck> gpNigeriaMarketCheckList = gpNigeriaMarketCheckAdd.getGpNigeriaMarketCheckList();
        if (CollectionUtils.isNotEmpty(gpNigeriaMarketCheckList)){
            for (int i = 0; i < gpNigeriaMarketCheckList.size(); i++) {
                GpNigeriaMarketCheck gpNigeriaMarketCheck = gpNigeriaMarketCheckList.get(i);
                if (CollectionUtils.isNotEmpty(gpNigeriaMarketCheck.getPackagesInformationList())){
                    //核对打包信息列表
                    this.checkPackagesInformations(gpNigeriaMarketCheck);
                }
                if (CollectionUtils.isNotEmpty(gpNigeriaMarketCheck.getSackInformationList())){
                    //核对散货信息列表
                    this.checkSackInformation(gpNigeriaMarketCheck);
                }

            }
        }
        return true;
    }

    private void checkSackInformation(GpNigeriaMarketCheck gpNigeriaMarketCheck) {
        for (GpSackInformation gpSackInformation : gpNigeriaMarketCheck.getSackInformationList()) {
            GpNigeriaMarketCheck nigeriaMarketCheck = Convert.convert(new TypeReference<GpNigeriaMarketCheck>() {}, gpSackInformation);
            //nigeriaMarketCheck.setSackId(gpSackInformation.getId());
            //nigeriaMarketCheck.setCtnLoadedId(gpSackInformation.getCtnLoadedId());
            //nigeriaMarketCheck.setCtnOrderId(gpSackInformation.getCtnOrderId());
            nigeriaMarketCheck.setIsCheck("1");
            nigeriaMarketCheck.setId(null);

            //更新散货信息是否核对
            GpSackInformation sackInformation = gpSackInformationService.getById(gpSackInformation.getId());
            sackInformation.setIsCheck("1");
            gpSackInformationService.updateById(sackInformation);

            //更新整个装柜信息核对情况
            int checkCount = gpSackInformationService.count(new QueryWrapper<GpSackInformation>().eq("receipt_id", gpSackInformation.getReceiptId()).eq("ctn_order_id", gpSackInformation.getCtnOrderId()));
            int checkedCount = gpSackInformationService.count(new QueryWrapper<GpSackInformation>().eq("receipt_id", gpSackInformation.getReceiptId()).eq("ctn_order_id", gpSackInformation.getCtnOrderId()).eq("is_check", 1));
            if (checkCount==checkedCount){
                //全部核对
                GpCtnOrder gpCtnOrdered = gpCtnOrderService.getById(gpNigeriaMarketCheck.getId());
                gpCtnOrdered.setIsCheck("1");
                gpCtnOrderService.updateById(gpCtnOrdered);
            }
            if (checkCount<checkedCount){
                //部分核对
                GpCtnOrder gpCtnOrdered = gpCtnOrderService.getById(gpNigeriaMarketCheck.getId());
                gpCtnOrdered.setIsCheck("2");
                gpCtnOrderService.updateById(gpCtnOrdered);
            }
            this.save(nigeriaMarketCheck);
        }


    }


    private void checkPackagesInformations(GpNigeriaMarketCheck gpNigeriaMarketCheck) {
        for (GpPackagesInformation gpPackagesInformation : gpNigeriaMarketCheck.getPackagesInformationList()) {
            GpNigeriaMarketCheck nigeriaMarketCheck = Convert.convert(new TypeReference<GpNigeriaMarketCheck>() {}, gpPackagesInformation);

            //nigeriaMarketCheck.setPackId(gpPackagesInformation.getId());
            //nigeriaMarketCheck.setCtnLoadedId(gpPackagesInformation.getCtnLoadedId());
            //nigeriaMarketCheck.setCtnOrderId(gpPackagesInformation.getCtnOrderId());
            nigeriaMarketCheck.setIsCheck("1");
            nigeriaMarketCheck.setId(null);
            //更新打包信息是否核对
            GpPackagesInformation packagesInformation = gpPackagesInformationService.getById(gpPackagesInformation.getId());
            packagesInformation.setIsCheck("1");
            gpPackagesInformationService.updateById(packagesInformation);

            //更新整个装柜信息核对情况
            int checkCount = gpPackagesInformationService.count(new QueryWrapper<GpPackagesInformation>().eq("receipt_id", gpPackagesInformation.getReceiptId()).eq("ctn_order_id", gpPackagesInformation.getCtnOrderId()));
            int checkedCount = gpPackagesInformationService.count(new QueryWrapper<GpPackagesInformation>().eq("receipt_id", gpPackagesInformation.getReceiptId()).eq("ctn_order_id", gpPackagesInformation.getCtnOrderId()).eq("is_check", 1));
            if (checkCount==checkedCount){
                //全部核对
                GpCtnOrder gpCtnOrdered = gpCtnOrderService.getById(gpNigeriaMarketCheck.getId());
                gpCtnOrdered.setIsCheck("1");
                gpCtnOrderService.updateById(gpCtnOrdered);
            }
            if (checkCount<checkedCount){
                //部分核对
                GpCtnOrder gpCtnOrdered = gpCtnOrderService.getById(gpNigeriaMarketCheck.getId());
                gpCtnOrdered.setIsCheck("2");
                gpCtnOrderService.updateById(gpCtnOrdered);
            }

            this.save(nigeriaMarketCheck);

        }

    }
}
